package com.back.end.Controller.Web;

import com.back.end.Modal.ProductChild;
import com.back.end.Modal.CategoriesChild;
import com.back.end.Modal.Sales;
import com.back.end.Service.WebProductService;
import com.back.end.enums.ResultCode;
import com.back.end.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/Web/Product")
@CrossOrigin
public class WebProductController {
    @Resource
    private WebProductService webProductService;

    @GetMapping("/productList")
    public Result getProductList() {
        List<ProductChild> productChildList = webProductService.getProductList();
        return Result.success(productChildList);
    }

    @RequestMapping("/productID")
    public Result getProductByID(Integer id) {
        ProductChild productChild = webProductService.getProductById(id);
        return Result.success(productChild);
    }

    @GetMapping("/productName")
    public Result getProductByName(String name) {
        ProductChild productChild = webProductService.getProductByName(name);
        return Result.success(productChild);
    }

    @GetMapping("/categories")
    public Result getProductType() {
        List<CategoriesChild> categoriesChildList = webProductService.getProductTypes();
        return Result.success(categoriesChildList);
    }

    @RequestMapping("/categoryId")
    public Result getCategory(Integer id) {
        CategoriesChild categoriesChild = webProductService.getCategoryById(id);
        return Result.success(categoriesChild);
    }

    @RequestMapping("/updateCategory")
    public Result updateCategory(Integer id, String name) {
        CategoriesChild categoriesChild = new CategoriesChild();
        ProductChild productChild = new ProductChild();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        categoriesChild.setId(id);
        categoriesChild.setName(name);
        categoriesChild.setUpdatedTime(timestamp);
        productChild.setName(name);
        productChild.setTypeId(id);
        webProductService.updateCategory(categoriesChild);
        return Result.success("成功");
    }

    @RequestMapping("/addCategory")
    public Result addCategory(String name) {
        CategoriesChild categoriesChild = new CategoriesChild();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        categoriesChild.setName(name);
        categoriesChild.setCreatedTime(now);
        webProductService.addCategory(categoriesChild);
        return Result.success("添加成功");
    }

    @PostMapping("/addProduct")
    public Result addProduct(String productCode, String name, int typeId, String type, double price, String detail, String imagePath) {
        ProductChild productChild = new ProductChild();
        productChild.setName(name);
        productChild.setProductCode(productCode);
        productChild.setTypeId(typeId);
        productChild.setPrice(price);
        productChild.setImagePath(imagePath);
        productChild.setType(type);
        productChild.setDetail(detail);
        String res = webProductService.addProduct(productChild);
        if (res.equals("成功")) {
            return Result.success("成功");
        } else {
            return Result.error(ResultCode.NAME_ERROR);
        }
    }

    @RequestMapping("/updateProduct")
    public Result updateProduct(int id, String productCode, String name, int typeId, String type, double price, String detail, String imagePath) {
        ProductChild productChild = new ProductChild();
        productChild.setId(id);
        productChild.setName(name);
        productChild.setProductCode(productCode);
        productChild.setTypeId(typeId);
        productChild.setPrice(price);
        productChild.setDetail(detail);
        productChild.setImagePath(imagePath);
        productChild.setType(type);
        webProductService.updateProduct(productChild);
        return Result.success("成功");
    }

    @RequestMapping("/dealProduct")
    public Result dealProduct(int id, String status) {
        ProductChild productChild = new ProductChild();
        productChild.setId(id);
        productChild.setStatus(status);
        webProductService.dealProduct(productChild);
        return Result.success("成功");
    }

    @RequestMapping("/getRankList")
    public Result getRankList(String time) {
        List<Sales> products = webProductService.getRankList(time);
        return Result.success(products);
    }


}
